<?php
require_once(DIR_LIBRARY."orm".DS."ctp".DS."thread.php");
require_once(DIR_LIBRARY."orm".DS."ctp".DS."post.php");
class Ctp
{
    public function createThread($app, $type, $creator)
    {
        $thread = CtpThread::factory();
        $thread->setApp($app);
        $thread->setType($type);
        $thread->setCreator($creator);
        $thread->create();

		return $thread;
    }

    public function createPost($thread, $creator, $body, $meta)
    {
        $post  = CtpPost::factory();
        $post->setBody($body);
        $post->setCreator($creator);
        $post->setMeta($meta);
        $post->create($thread);

		return $post;
    }

    public function updateThread($thread_id, $body)
    {
        $thread = CtpThread::factory();
        $thread->byId($thread_id);

        return $thread->update();
    }

    public function updatePost($thread_id, $sequence, $body)
    {
        $post = CtpPost::factory();
        $post->appendWhere('thread_id="'.$thread_id.'" AND sequence = "'.$sequence.'"');
        $post->setBody($body);

        return $post->update();
    }

	public function deletePost($thread_id, $sequence)
	{
        $post = CtpPost::factory();
        $post->appendWhere('thread_id="'.$thread_id.'" AND sequence = "'.$sequence.'"');

        return $post->delete();
	}
}
